package com.manager.user.module;


import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.manager.commons.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;

/**
 * 验证码
 *
 * @author H
 * @version 1.0
 */
@Controller
public class CaptchaController {

    @Autowired
    private Producer producer;

    /**
     * 获取验证码图片
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/captcha")
    public void getCaptchaCode(HttpServletRequest request, HttpServletResponse response) {
        String time = request.getParameter("time");
        if (StringUtil.isNotEmpty(time)) {
            HttpSession session = request.getSession();
            String stime = StringUtil.toString(session.getAttribute(Constants.KAPTCHA_SESSION_DATE));
            if (stime.equals(time)) {//如果时间
                return;
            }
            response.setDateHeader("Expires", 0);
            // Set standard HTTP/1.1 no-cache headers.
            response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
            // Set IE extended HTTP/1.1 no-cache headers (use addHeader).
            response.addHeader("Cache-Control", "post-check=0, pre-check=0");
            // Set standard HTTP/1.0 no-cache header.
            response.setHeader("Pragma", "no-cache");
            // return a jpeg
            response.setContentType("image/jpeg");
            // 生成验证码文本
            String text = producer.createText();

            String capText = text;
            int count1 = Integer.parseInt(text.substring(0, 1));
            int count2 = Integer.parseInt(text.substring(1, 2));
            int capCount = count1 + count2;
            String operator = "+";
            if (count1 > count2) {
                capCount = count1 - count2;
                operator = "-";
            }
            capText = count1 + operator + count2 + "=?";
            session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capCount);
            session.setAttribute(Constants.KAPTCHA_SESSION_DATE, time);

            ServletOutputStream out = null;
            try {
                // 利用生成的字符串构建图片
                BufferedImage bi = producer.createImage(capText);
                out = response.getOutputStream();
                ImageIO.write(bi, "jpg", out);
                out.flush();
            } catch (Exception e) {
            } finally {
                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException e) {

                    }
                }
            }
        }

        // return null;
    }

}